<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 7.1: Logistic regression (GP version)</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch07_statistical_estim/html/logistics_gp.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 7.1: Logistic regression (GP version)</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Section 7.1.1</span>
<span class="comment">% Boyd &amp; Vandenberghe, "Convex Optimization"</span>
<span class="comment">% Kim &amp; Mutapcic, "Logistic regression via geometric programming"</span>
<span class="comment">% Written for CVX by Almir Mutapcic 02/08/06</span>
<span class="comment">%</span>
<span class="comment">% Solves the logistic regression problem re-formulated as a GP.</span>
<span class="comment">% The original log regression problem is:</span>
<span class="comment">%</span>
<span class="comment">%   minimize   sum_i(theta'*x_i) + sum_i( log(1 + exp(-theta'*x_i)) )</span>
<span class="comment">%</span>
<span class="comment">% where x are explanatory variables and theta are model parameters.</span>
<span class="comment">% The equivalent GP is obtained by the following change of variables:</span>
<span class="comment">% z_i = exp(theta_i). The log regression problem is then a GP:</span>
<span class="comment">%</span>
<span class="comment">%   minimize   prod( prod(z_j^x_j) ) * (prod( 1 + prod(z_j^(-x_j)) ))</span>
<span class="comment">%</span>
<span class="comment">% with variables z and data x (explanatory variables).</span>

randn(<span class="string">'state'</span>,0);
rand(<span class="string">'state'</span>,0);

a =  1;
b = -5;

m = 100;
u = 10*rand(m,1);
y = (rand(m,1) &lt; exp(a*u+b)./(1+exp(a*u+b)));

<span class="comment">% order the observation data</span>
ind_false = find( y == 0 );
ind_true  = find( y == 1 );

<span class="comment">% X is the sorted design matrix</span>
<span class="comment">% first have true than false observations followed by the bias term</span>
X = [u(ind_true); u(ind_false)];
X = [X ones(size(u,1),1)];
[m,n] = size(X);
q = length(ind_true);

cvx_begin <span class="string">gp</span>
  <span class="comment">% optimization variables</span>
  variables <span class="string">z(n)</span> <span class="string">t(q)</span> <span class="string">s(m)</span>

  minimize( prod(t)*prod(s) )
  subject <span class="string">to</span>
    <span class="keyword">for</span> k = 1:q
      prod( z.^(X(k,:)') ) &lt;= t(k);
    <span class="keyword">end</span>

    <span class="keyword">for</span> k = 1:m
      1 + prod( z.^(-X(k,:)') ) &lt;= s(k);
    <span class="keyword">end</span>
cvx_end

<span class="comment">% retrieve the optimal values and plot the result</span>
theta = log(z);
aml = -theta(1);
bml = -theta(2);

us = linspace(-1,11,1000)';
ps = exp(aml*us + bml)./(1+exp(aml*us+bml));

plot(us,ps,<span class="string">'-'</span>, u(ind_true),y(ind_true),<span class="string">'o'</span>, <span class="keyword">...</span>
                u(ind_false),y(ind_false),<span class="string">'o'</span>);
axis([-1, 11,-0.1,1.1]);
</pre>
<a id="output"></a>
<pre class="codeoutput">
 
Successive approximation method to be employed.
   sedumi will be called several times to refine the solution.
   Original size: 656 variables, 401 equality constraints
   200 exponentials add 1600 variables, 1000 equality constraints
-----------------------------------------------------------------
 Cones  |             Errors              |
Mov/Act | Centering  Exp cone   Poly cone | Status
--------+---------------------------------+---------
200/200 | 5.361e+00  2.242e+00  1.198e-09 | Solved
200/200 | 4.227e-01  1.259e-02  1.428e-09 | Solved
200/200 | 4.361e-02  1.353e-04  1.858e-09 | Solved
117/164 | 1.943e-03  1.780e-07  1.938e-09 | Solved
  1/112 | 8.779e-04  1.938e-09  1.938e-09 | Solved
  0/111 | 1.143e-06  1.939e-09  1.937e-09 | Solved
-----------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): +2.10331e+14
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="logistics_gp__01.png" alt=""> 
</div>
</div>
</body>
</html>